// DlgOptions.cpp : implementation file
//

#include "stdafx.h"
#include "ansieditor.h"
#include "DlgOptions.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgOptions dialog


CDlgOptions::CDlgOptions(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgOptions::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgOptions)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pCurPage = NULL;
}


void CDlgOptions::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgOptions)
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_LIST_PAGE, m_listPage);
}


BEGIN_MESSAGE_MAP(CDlgOptions, CDialog)
	//{{AFX_MSG_MAP(CDlgOptions)
	ON_WM_CREATE()
	ON_LBN_SELCHANGE(IDC_LIST_PAGE, OnSelchangeListPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgOptions message handlers

BOOL CDlgOptions::OnInitDialog() 
{
//	m_listPage.SubclassDlgItem(IDC_LIST_PAGE, this);
	CDialog::OnInitDialog();
	m_listPage.AddItem(gpApp->LoadIcon(IDI_GENERAL), gpApp->m_mui.LoadString(IDS_OPTION_GENERAL));
	m_listPage.AddItem(gpApp->LoadIcon(IDI_EDIT), gpApp->m_mui.LoadString(IDS_OPTION_EDIT));
	m_listPage.AddItem(gpApp->LoadIcon(IDI_DISPLAY), gpApp->m_mui.LoadString(IDS_OPTION_DISPLAY));
	m_listPage.AddItem(gpApp->LoadIcon(IDI_ANIMATE), gpApp->m_mui.LoadString(IDS_OPTION_ANIMATE));
	m_listPage.AddItem(gpApp->LoadIcon(IDI_OUTPUT), gpApp->m_mui.LoadString(IDS_OPTION_OUTPUT));
	m_listPage.AddItem(gpApp->LoadIcon(IDI_PLUGINS), gpApp->m_mui.LoadString(IDS_OPTION_PLUGINS));
	m_listPage.AddItem(gpApp->LoadIcon(IDI_SCRIPT), gpApp->m_mui.LoadString(IDS_OPTION_SCRIPT));

	m_arrPage.Add(&m_pageGeneral);
	m_arrPage.Add(&m_pageEdit);
	m_arrPage.Add(&m_pageDisplay);
	m_arrPage.Add(&m_pageAnimate);
	m_arrPage.Add(&m_pageOutput);
	m_arrPage.Add(&m_pagePlugins);
	m_arrPage.Add(&m_pageScript);
	
	int nPage = gpApp->GetProfileInt("Dialog", "Options.LastPage", 0);
	m_listPage.SetCurSel(nPage);
	OnSelchangeListPage();

	gpApp->m_mui.UpdateDialog(this, IDD);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CDlgOptions::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	return 0;
}

void CDlgOptions::OnSelchangeListPage()
{
	if(m_pCurPage)
	{
		m_pCurPage->ShowWindow(SW_HIDE);
	}
	int nPage = m_listPage.GetCurSel();
	CPrPage *pPage = m_arrPage.GetAt(nPage);
	if(!::IsWindow(pPage->GetSafeHwnd()))
	{
		pPage->Create(this);
		CRect rect;
		GetDlgItem(IDC_FRAME_PRPAGE)->GetWindowRect(rect);
		ScreenToClient(rect);
		rect.DeflateRect(5, 12, 5, 5);
		pPage->MoveWindow(rect);
	}
	m_pCurPage = pPage;
	
	CString sTitle;
	HWND hwndLblTitle = ::GetDlgItem(m_pCurPage->GetSafeHwnd(), IDC_LBL_TITLE);
	if(hwndLblTitle)
	{
		CWnd::FromHandle(hwndLblTitle)->GetWindowText(sTitle);
	}
	else
	{
		m_listPage.GetText(nPage, sTitle);
	}
	SetDlgItemText(IDC_FRAME_PRPAGE, sTitle);

	m_pCurPage->SetWindowPos(&m_listPage, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
}

void CDlgOptions::OnOK() 
{
	int nCount = m_arrPage.GetSize();
	for(int n = 0; n < nCount; n++)
	{
		CPrPage *pPage = m_arrPage.GetAt(n);
		if(::IsWindow(pPage->GetSafeHwnd()))
		{
			if(!pPage->ValidateData())
			{
				m_listPage.SetCurSel(n);
				OnSelchangeListPage();
				return;
			}
		}
	}
	gpApp->m_options.Save();
	gpApp->WriteProfileInt("Dialog", "Options.LastPage", m_listPage.GetCurSel());
	CDialog::OnOK();
}

void CDlgOptions::OnCancel() 
{
	int nCount = m_arrPage.GetSize();
	for(int n = 0; n < nCount; n++)
	{
		CPrPage *pPage = m_arrPage.GetAt(n);
		if(::IsWindow(pPage->GetSafeHwnd()))
		{
			pPage->Cancel();
		}
	}
	
	CDialog::OnCancel();
}
